<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Product extends MY_Controller {

     function __construct() {
        
		parent::__construct();
        $this->load->helper('url');
        $this->load->library('parser');
        $this->load->helper('util');
        $this->load->model('product_model', 'product', true);
        $this->load->model('pcategory_model', 'category', true);

        $this->buildNavigation();
        $this->buildSearchByConditionBox();
        
		//$this->buildSlider();
		
     }

    function index($code = '') {
        $this->view($code);
    }

    function view($code = "", $offset = 0, $number_record = 30) {
		
    	$this->buildSupportBox();
		$this->buildCartBox();
		$this->buildCompareBox();
		$this->buildAdvBox();
		
    	$this->load->library('pagination');
    	
        // echo '<br>' . $code_category . '<br>';
        //$category = $this->category->getByCode($code);
        $cat = -1;
        $catIds = -1;
        $cCat = 0;
        if(!empty($code)){
        	$cat = $this->category->getByCode($code);
    		$cCat = $cat['id_pcategory'];
       		$catIds = getElementsOfNode($cat['id_pcategory']);
    		$catIds[] = $cat['id_pcategory'];
        }
            
    	$offset = (int) $offset;
        //$base_url = $this->base_url . 'admin/product/view/' . $category . '/';
        $base_url = site_url ( 'product/view/' . $code . '/');
        $total_rows = $this->product->countAll($catIds);
        $per_page = 24; //NUMBER_RECORD_PER_PRODUCT_PAGE;
        $uri_segment = 4;

		
		
        pagination_front($base_url, $total_rows, $per_page, $uri_segment);
        
    	$data['site_url'] = site_url();
		
       	$data['base_url'] = base_url();
       
    	if(!empty($code)){
			
    		$data['products'] = $this->product->getAll($offset, $per_page, $catIds);
    	}
    	else {
    		$data['products'] = $this->product->getNewProduct();
    	}
   
        $lang = getLangCode();
        
       	$cats = getTrack($cCat, null, $lang);
        
        $data['cats'] = $cats;
        
        $data['lang'] =  $lang;
        
        //$this->template->parse_view('center_content1', 'default/front_end/product/list_product_view', $data);
        $this->template->parse_view('center_content1', 'default/front_end/product/grid_product_view', $data);
        
        //$this->buildLeftContent();

        $this->template->render();  
        
    }
    
	function detail($code) {
		
    	$data = $this->product->getByCode($code);
    	
    	$lang = getLangCode();
    	
    	$data['pname'] = $data[$lang . '_name'];
    	$data['description'] = $data[$lang . '_description'];
    	
    	
    	$image = $data['image'];
		$upload_dir = getDirByDate($data['date_add'], PRODUCT_IMAGE_PATH);
		
		if(!file_exists($upload_dir.'/'.$image)) 
			$image = '';
		else 
			$image = img(array('src'=>base_url($upload_dir.'/' .$image), 'width' => '220px', 'height'=>'295px' ));
		
		$data['image'] = $image;
		$data['add_to_wishlist'] = lang('add_to_wishlist'); 
		$data['add_to_compare'] = lang('add_to_compare');
		$data['email_to_a_friend'] = lang('email_to_a_friend');
		$data['product_information'] = lang('product_information');
		$data['upload_dir'] = $upload_dir;
		
		$this->load->model('upload_files_model', 'file', true);
		$data['images'] =  $this->file->getFileByType($data['id_product'], IMAGE_TYPE, PRODUCT_TYPE);
		
        $lang = getLangCode();
        
        $cats = getTrack($data['id_pcategory'], null, $lang);
        
        $data['cats'] = $cats;
        
        $data['lang'] =  $lang;
        $data['site_url'] =  site_url();
        $data['base_url'] =  base_url();
        
        //$this->template->parse_view('center_content1', 'default/front_end/product/list_product_view', $data);
        //$this->template->parse_view('center_content1', 'default/front_end/product/detail_view', $data);
        $this->template->parse_view('top_content1', 'default/front_end/product/detail_view', $data);
        //$this->buildLeftContent();

        $this->template->render();  
        
    }

	function search($keyword = "", $cat_code = "", $offset = 0, $number_record = 30) {
		
    	$this->load->library('pagination');
    	
        $cat = -1;
        $catIds = -1;
        if(!empty($code)){
        	$cat = $this->category->getByCode($code);
    	
       		$catIds = getElementsOfNode($cat['id_pcategory']);
    		$catIds[] = $cat['id_pcategory'];
        }
            
    	$offset = (int) $offset;
        //$base_url = $this->base_url . 'admin/product/view/' . $category . '/';
        $base_url = site_url ( 'product/view/' . $code . '/');
        $total_rows = $this->product->countAll($catIds);
        $per_page = 24; //NUMBER_RECORD_PER_PRODUCT_PAGE;
        $uri_segment = 4;
        
        pagination_front($base_url, $total_rows, $per_page, $uri_segment);
        
    	$data['site_url'] = site_url();
		
       	$data['base_url'] = base_url();
       
    	if(!empty($code)){
			
    		$data['products'] = $this->product->getAll($offset, $per_page, $catIds);
    	}
    	else {
    		$data['products'] = $this->product->getNewProduct();
    	}
   
        $lang = getLangCode();
        
        $data['lang'] =  $lang;
        
        //$this->template->parse_view('center_content1', 'default/front_end/product/list_product_view', $data);
        $this->template->parse_view('center_content1', 'default/front_end/product/grid_product_view', $data);
        //$this->buildLeftContent();

        $this->template->render();  
        
    }

}

?>